var marqueecontent = new Array();
var marqueeInterval = new Array();
var marqueeId = 0;
var marqueeDelay = 3000;
var marqueeHeight = 45;

function startMarquee() {
	var str = marqueecontent[marqueeId];
	marqueeId++;
	if (marqueeId >= marqueecontent.length)
		marqueeId = 0;

	if (document.getElementById("marqueeBox").childNodes.length == 1) {
		var nextLine = document.createElement('DIV');
		nextLine.innerHTML = str;
		document.getElementById("marqueeBox").appendChild(nextLine);
	} else {
		document.getElementById("marqueeBox").childNodes[0].innerHTML = str;
		document.getElementById("marqueeBox").appendChild(document.getElementById("marqueeBox").childNodes[0]);
		document.getElementById("marqueeBox").scrollTop = 0;
	}
	clearInterval(marqueeInterval[1]);
	marqueeInterval[1] = setInterval("scrollMarquee()", 20);
}

function scrollMarquee() {
	document.getElementById("marqueeBox").scrollTop++;
	if (document.getElementById("marqueeBox").scrollTop % marqueeHeight == (marqueeHeight - 1)) {
		clearInterval(marqueeInterval[1]);
	}
}




$(document).ready(function() {
	
	var strDate = '';//年月日
	var strMonth = '';//年月

	function initDate(){
		var now = new Date();
		var year = now.getFullYear();
		var month = now.getMonth() + 1;
		var day = now.getDate();
		var sm = '';
		var sd = '';
		if (month < 10) {
			sm = '0' + month;
		} else {
			sm = '' + month;
		}
		if (day < 10) {
			sd = '0' + day;
		} else {
			sd = '' + day;
		}
		strDate = year+'-'+sm+'-'+sd;//年月日
		strMonth = year+'-'+sm;//年月
	}
	initDate();
	
	
	$.InitChart5 = function(){
		clearInterval(marqueeInterval[0]);
		clearInterval(marqueeInterval[1]);
		marqueecontent = new Array();
		marqueeInterval = new Array();
		marqueeId = 0;
		marqueeDelay = 3000;
		marqueeHeight = 37;

		jQuery.ajax({
			url: "adminScreen.do?action=LargeScreenAction&event_submit_do_queryAlarmMeter=method",
			type: "post",
			dataType: "json",
			success: function(obj) {

				if (obj.length > 0) {

					for (var i = 0; i < obj.length; i++) {
						var o = obj[i];
						var temp = '';
						if (o.ecCode == 'water') {
							if (o.buildId == 'collector') {
								temp = '水表采集器';
							} else {
								temp = '水表'
							}
						} else {
							if (o.buildId == 'collector') {
								temp = '电表采集器';
							} else {
								temp = '电表'
							}
						}
						var shour = (o.occurTime).substring(11, 16);

						var s = shour + ' [' + temp + '<' + o.collectorSn + '>] ' + o.alarmContent;

						marqueecontent[i] = s;

					}
					initMarquee();
				}
			},
			error: function(jqXHR, textStatus, errorThrown) {
			}
		});
	}	
	$.InitChart5();
	

	function initMarquee() {
		$('#marqueeBoxDiv #marqueeBox').remove();
		var str = marqueecontent[0];
		$('#marqueeBoxDiv').append('<div id="marqueeBox" style="float:left;font-weight:bold; line-height: 37px;font-size: 18px;font-family: 微软雅黑; height:' + marqueeHeight + 'px" onmouseover="clearInterval(marqueeInterval[0])" onmouseout="marqueeInterval[0]=setInterval(\'startMarquee()\',marqueeDelay)"><div>' + str + '</div></div>');		
		marqueeId++;
		tt = true;
		clearInterval(marqueeInterval[0]);
		marqueeInterval[0] = setInterval("startMarquee()", marqueeDelay);
	}



	//创始化图表
	var myChart = echarts.init(document.getElementById('main1'), theme22);
	//动态请求图表数据
	//日期，建筑id,总电能分项，总水能分项
	function InitChart(strDate, buildId, eiIde, eiIdw) {
		$.ajax({
			url: "adminScreen.do?action=LargeScreenAction&event_submit_do_query_data_day=method",
			data: {
				date: strDate,
				buildId: buildId,
				eiIde: eiIde,
				eiIdw: eiIdw
			},
			type: "post",
			dataType: "json",
			success: function(obj) {
				if (obj.xAxisData.length > 0)

					BindChart(obj);
				//BindChart1111();
			},
			error: function(jqXHR, textStatus, errorThrown) {

			}
		});
	}
	//封装图表数据格式
	function BindChart(obj) {
		myChart.clear();
		var ydata = obj.yAxisData[0].data; // y轴数据集合(电能)
		var ydata2 = obj.yAxisData[1].data; // y轴数据集合（水能）
		var ylength = obj.yAxisData[0].data.length;
		var pp = new Array(ylength); //重新定义y轴数据集合
		for (var i = 0; i < ylength; i++) {
			if (ydata[i] != null)
				pp[i] = ydata[i];
		}

		var pp2 = new Array(ylength);
		for (var y = 0; y < ylength; y++) {
			if (ydata2[y] != null)
				pp2[y] = ydata2[y];
		}

		obj.yAxisData[0].data = pp
		obj.yAxisData[1].data = pp2

		var option = {
			tooltip: {
				trigger: 'axis',
				textStyle:{
                    align:'left'
                }
			},
			grid: {
				x: 50,
				y: 30,
				x2: 50,
				y2: 30
			},
			legend: {
				data: obj.legendData
			},
			toolbox: {
				show: true
			},
			calculable: true,
			xAxis: [{
				type: 'category',
				boundaryGap: false,
				axisLabel: {
					textStyle: {
						color: '#ffffff'
					}
				},
				splitLine: {
					show: false,
					lineStyle: {
						color: '#ff0000',
						width: 1
					}
				},
				axisLine: { //x轴线
					show: true,
					lineStyle: {
						color: '#505050',
						width: 1
					}
				},

				data: obj.xAxisData,
			}],
			yAxis: [{
				type: 'value',
				axisLabel: {
					formatter: '{value}',
					textStyle: {
						color: '#4D7871'
					}
				},
				splitLine: { //分隔线
					show: false,
					lineStyle: {
						color: '#ff0000',
						width: 1
					}

				},
				axisLine: { //x轴线
					show: true,
					lineStyle: {
						color: '#505050',
						width: 1
					}
				},

				name: '单位：度',
				nameTextStyle: {
					color: '#ffffff'
				}
			}, {
				type: 'value',
				axisLabel: {
					formatter: '{value}',
					textStyle: {
						color: '#BFB68B'
					}
				},
				splitLine: { //分隔线
					show: false,
					lineStyle: {
						color: '#ff0000',
						width: 1
					}
				},
				axisLine: { //x轴线
					show: true,
					lineStyle: {
						color: '#505050',
						width: 1
					}
				},
				name: '单位：吨',
				nameTextStyle: {
					color: '#ffffff'
				}
			}],
			series: obj.yAxisData

		};
		// 为echarts对象加载数据 
		myChart.setOption(option);
	}


	//今日能耗实时更新视图  
	InitChart(strDate, buildId, eiIde,eiIdw);

	
	
	
	var myChart2 = echarts.init(document.getElementById('main2'), theme22);

	function InitChart2(strDate, buildId, eiId) {
		jQuery.ajax({
			url: "adminScreen.do?action=LargeScreenAction&event_submit_do_query_data=method",
			data: {
				date: strDate,
				buildId: buildId,
				eiId: eiId
			},
			type: "post",
			dataType: "json",
			success: function(obj) {
				if (obj.xAxisData.length > 0)

					BindChart2(obj);
			},
			error: function(jqXHR, textStatus, errorThrown) {

			}
		});
	}
	//封装图表数据格式
	function BindChart2(obj) {
		myChart2.clear();
		var ydata = obj.yAxisData[0].data; // y轴数据集合(电能)
		//var ydata2 = obj.yAxisData[1].data;// y轴数据集合（电能累计值）
		var ylength = obj.yAxisData[0].data.length;
		var pp = new Array(ylength); //重新定义y轴数据集合
		for (var i = 0; i < ylength; i++) {
			if (ydata[i] != null)
				pp[i] = ydata[i];
		}
		obj.yAxisData[0].data = pp
		var option = {
			tooltip: {
				trigger: 'axis',
				textStyle:{
                    align:'left'
                }
			},
			grid: {
				x: 50,
				y: 30,
				x2: 10,
				y2: 30
			},
			legend: {
				data: obj.legendData
			},
			toolbox: {
				show: true
			},
			calculable: true,
			xAxis: [{
				type: 'category',
				boundaryGap: false,
				data: obj.xAxisData,
				axisLabel: {
					textStyle: {
						color: '#ffffff'
					}
				},
				splitLine: {
					show: true,
					lineStyle: {
						color: '#1E1E1E',
						width: 1
					}
				},
				axisLine: { // 轴线
					show: true,
					lineStyle: {
						color: '#505050',
						width: 1
					}
				},

			}],
			yAxis: [{
				type: 'value',
				axisLabel: {
					formatter: '{value}',
					textStyle: {
						color: '#4D7871'
					}
				},
				name: '单位：度',
				nameTextStyle: {
					color: '#ffffff'
				},
				splitLine: {
					show: true,
					lineStyle: {
						color: '#1E1E1E',
						width: 1
					}
				},
				axisLine: { // 轴线
					show: true,
					lineStyle: {
						color: '#505050',
						width: 1
					}
				},
			}, ],
			series: obj.yAxisData

		};
		// 为echarts对象加载数据 
		myChart2.setOption(option);
	}
	
	InitChart2(strMonth, buildId, eiIde);

	var myChart3 = echarts.init(document.getElementById('main3'), theme22);

	function InitChart3(strDate, buildId, eiId) {
		jQuery.ajax({
			url: "adminScreen.do?action=LargeScreenAction&event_submit_do_query_data=method",
			data: {
				date: strDate,
				buildId: buildId,
				eiId: eiId
			},
			type: "post",
			dataType: "json",
			success: function(obj) {
				if (obj.xAxisData.length > 0)

					BindChart3(obj);
			},
			error: function(jqXHR, textStatus, errorThrown) {

			}
		});
	}
	//封装图表数据格式
	function BindChart3(obj) {
		myChart3.clear();
		var ydata = obj.yAxisData[0].data; // y轴数据集合(电能)
		//var ydata2 = obj.yAxisData[1].data;// y轴数据集合（电能累计值）
		var ylength = obj.yAxisData[0].data.length;
		var pp = new Array(ylength); //重新定义y轴数据集合
		for (var i = 0; i < ylength; i++) {
			if (ydata[i] != null)
				pp[i] = ydata[i];
		}
		
		obj.yAxisData[0].data = pp

		var option = {
			tooltip: {
				trigger: 'axis',
				textStyle:{
                    align:'left'
                }
			},
			grid: {
				x: 50,
				y: 30,
				x2: 10,
				y2: 30
			},
			legend: {
				data: obj.legendData
			},
			toolbox: {
				show: true
			},
			calculable: true,
			xAxis: [{
				type: 'category',
				boundaryGap: false,
				data: obj.xAxisData,
				axisLabel: {
					textStyle: {
						color: '#ffffff'
					}
				},
				splitLine: {
					show: true,
					lineStyle: {
						color: '#1E1E1E',
						width: 1
					}
				},
				axisLine: { // 轴线
					show: true,
					lineStyle: {
						color: '#505050'
					}
				},
			}],
			yAxis: [{
				type: 'value',
				axisLabel: {
					formatter: '{value}',
					textStyle: {
						color: '#BFB68B'
					}
				},
				name: '单位：吨',
				nameTextStyle: {
					color: '#ffffff'
				},
				splitLine: {
					show: true,
					lineStyle: {
						color: '#1E1E1E',
						width: 1
					}
				},
				axisLine: { // 轴线
					show: true,
					lineStyle: {
						color: '#505050',
						width: 1
					}
				},
			}, ],
			series: [{
					name: '日',
					type: 'line',
					data: obj.yAxisData[0].data,
					itemStyle: {
						normal: {
							color: function(params) {
								var colorList = [
									'#9BCA63'
								];
								return colorList[0]
							}
						}
					}
				}]
				//obj.yAxisData

		};
		// 为echarts对象加载数据 
		myChart3.setOption(option);
	}
	InitChart3(strMonth, buildId,eiIdw);

	//设备故障信息显示区域
	//InitChart4();
	//setInterval(InitChart4, 300000); //5分钟从后台刷新数据
	setInterval(function(){
		initDate();
		$.InitChart5();	
		InitChart(strDate, buildId, eiIde,eiIdw);
		InitChart2(strMonth, buildId, eiIde);
		InitChart3(strMonth, buildId,eiIdw);
	}, 1000 * 60 * time); //5分钟从后台刷新数据
	
	
});